"""List of diffent quote periods existing throught the system.

This data class is used both to pass parameter to the database and infoGetter
and to the Store. The Minutes* and Hourly periods are calculated on-the-fly

"""
import math
import numpy as np
import datetime
dt = datetime.datetime # Shortcut

secondly = 0
min1 = 1
min3 = 2
min5 = 3
min10 = 4
min15 = 5
min30 = 6
min60 = 7 # min60 and hourly are the same thing
hourly = 7
daily = 8
weekly = 9
monthly = 10
bimonthly = 11
quarterly = 12
semesterly = 13
yearly = 14

bod = dt.combine(datetime.date.today(), datetime.time()) # beginning of day

# Positions with a value of -1 mean that that field is not applicable for the
# respective row
data = np.array([
("Secondly",   "secondly",   bod,                 -1,  -1, -1, 25200),
("1-minute",   "min1",       dt(2012, 1, 1, 0, 0), 1,   1, -1,   450),
("3-minute",   "min3",       dt(2012, 1, 1, 0, 0), 3,   3, -1,   150),
("5-minute",   "min5",       dt(2012, 1, 1, 0, 0), 6,   5, -1,    90),
("10-minute",  "min10",      dt(2012, 1, 1, 0, 0), 12, 10, -1,    45),
("15-minute",  "min15",      dt(2012, 1, 1, 0, 0), 12, 15, -1,    30),
("30-minute",  "min30",      dt(2012, 1, 1, 0, 0), 24, 30, -1,    15),
("Hourly",     "hourly",     dt(2012, 1, 1, 0, 0), 60, 60, -1,     8),
("Daily",      "daily",      dt(1990, 1, 1, 0, 0), -1, -1, -1,     1),
("Weekly",     "weekly",     dt(1990, 1, 1, 0, 0), -1, -1, -1,     1./5),
("Monthly",    "monthly",    dt(1990, 1, 1, 0, 0), -1, -1,  1,     1./22),
("Bimonthly",  "bimonthly",  dt(1990, 1, 1, 0, 0), -1, -1,  2,     1./22/2),
("Quarterly",  "quarterly",  dt(1990, 1, 1, 0, 0), -1, -1,  3,     1./22/3),
("Semesterly", "semesterly", dt(1990, 1, 1, 0, 0), -1, -1,  6,     1./22/6),
("Yearly",     "yearly",     dt(1990, 1, 1, 0, 0), -1, -1, 12,     1./22/12),
], dtype=[("english", "|S10"),
          ("varname", "|S10"),
          ("start", dt),
          ("mpt", 'i4'), # Months Per Table - divider or multiple of 12
          ("nm", "i4"), # Number of minutes 
          ("numMonths", "i4"), # Number of months
          ("numPointsPerDay", float), # Number of points per day, periods < daily
                                     #are based on a 7.5-hour market
         ])
#TODO
#def english2int(s):
#    - numpy search within recarray
#    - update config reading within screens.ScreensSet

def GetEnglish(period):
    return data[period]["english"]

def GetVarname(period):
    return data[period]["varname"]

def GetDefaultStart(period):
    """Provides a default start datetime.datetime for each different period."""
    return data[period]["start"]

def FindPeriod(varname):
    """Finds the integer period given varname or raises exception."""
#    try:
    period = data[:]["varname"].tolist().index(varname)
#    except:
#        period = None
    return period
        
    


def NumPoints2Timedelta(period, numPoints, precaution=1.1):
    """Converts number of points to time delta.
    
    This functions performs an approximate conversion between a desired number
    of points. The precaution factor inflates the estimation. It is better to have
    more than less points than desired.
    """

    days = numPoints*precaution/data[period]["numPointsPerDay"]
    return datetime.timedelta(days=math.ceil(days))
        
        
